import { defineStore } from 'pinia'
import { Local } from '../utils/storage'

export interface UserInfo {
  id: number
  username: string
  nickname: string
  email: string
  mobile: string
  gender: number
  birthday: string
  money: number
  score: number
  avatar: string
  last_login_time: string
  last_login_ip: string
  join_time: string
  motto: string
  token: string
  refresh_token: string
}


export const useUserInfo = defineStore('userInfo', {
    state: ()=> {
        return {
            user: {},
            token: '',
        }
    },
    actions: {
        dataFill(user: any) {
          //console.log('==========保存token')
          //this.$state = { ...this.$state, ...state }
          this.user = user

          // 缓存
          Local.set('userInfo', user)
        },
        setToken(token: string) {
          //console.log('==========保存token')
          //this.$state.token = token
          this.token = token
          // 缓存
          Local.set('token', token)
        },
        getToken() {
          return Local.get('token')
        },
        getUser () {
          return this.user
        },
        getGenderIcon() {
            let icon = { name: 'fa fa-transgender-alt', color: 'var(--el-text-color-secondary)' }
            switch (this.gender) {
                case 1:
                    icon = { name: 'fa fa-mars-stroke-v', color: 'var(--el-color-primary)' }
                    break
                case 2:
                    icon = { name: 'fa fa-mars-stroke', color: 'var(--el-color-danger)' }
                    break
            }
            return icon
        },
        isLogin() {
            return this.id && this.token
        },
    }
})
